import java.net.*;   
import java.io.*;   
import java.util.*;   
   
public class DatagramClient{   
     
   
    //size of the packet   
    public static final int SIZE = 1;   
   
    public static void main(String args[]){   
   
    	//UDP port to which service is bound   
        public static final int SERVICE_PORT = args[1];  
        
        //declare the times that the packets has been resent   
        int duplicated=0;   
   
        //declare the total packets that have been received   
        int total=0;   
        //Get an InetAddress for the specified hostname   
        InetAddress addr = null;   
        try{   
   
            //resolve the hostname to an InetAddr   
            addr = InetAddress.getByName(args[0]);   
        }   
        catch (UnknownHostException ue){   
   
            System.out.println("Unable to resolve host");   
            return;   
        }   
        try{   
   
            //Bind to any free port   
            DatagramSocket socket = new DatagramSocket();   
            //Set a timeout value of two seconds   
            socket.setSoTimeout(2000);   
            String message = "a";   
            char[] array = message.toCharArray();   
            //Get the start time   
            Calendar begintime =Calendar.getInstance();   
            for(int i=1;i<=1000;i++)   
            {   
                byte[] sendbuf = new byte[1];   
                sendbuf[0]= (byte)array[0];   
               
                //Creat a packet to sent to the UDP server   
                DatagramPacket sendPacket = new DatagramPacket(   
                                                    sendbuf,1,addr,SERVICE_PORT);   
                System.out.println("Sending packet to" + "localhost");   
   
                //Send the packet   
   
                socket.send(sendPacket);   
                System.out.println("Waiting for packet....");   
   
                //Creat a small packet for receving UDP packets   
                byte[] recbuf = new byte[SIZE];   
                DatagramPacket receivePacket = new DatagramPacket(recbuf,SIZE);   
   
                //Declare a timeout flag   
                boolean timeout = false;   
   
                //catch any InterruptedIOException that is thrown while waiting to recieve a UDP packet   
   
                try{   
   
                    socket.receive(receivePacket);   
                }   
                catch(InterruptedIOException ioe){   
   
                    timeout = true;   
                }   
                if(!timeout){   
   
                    System.out.println("packet received!");   
                    System.out.println("packet " + i + " is revieved");   
                    total=i;   
   
                }else{   
   
                    System.out.println("Packet " + i + "is lost!");   
                    //send the packet which is lost again   
                    socket.send(sendPacket);   
                    while(timeout&(duplicated<=3)){   
                           
                        //calculate the times it has been resend   
                        duplicated++;                      
                        System.out.println("Waiting for packet....");   
   
                        //Creat a small packet for receving the duplicated UDP packets which is lost   
                        byte[] dupbuf = new byte[SIZE];   
                        DatagramPacket dupreceivePacket = new DatagramPacket(dupbuf,SIZE);   
   
                        //set the timeout to be false   
                        timeout = false;   
   
                        //catch any InterruptedIOException that is thrown while waiting to recieve a UDP packet   
   
                        try{   
       
                            socket.receive(dupreceivePacket);   
                        }   
                        catch(InterruptedIOException ioe){   
   
                            timeout = true;   
                        }   
                    }   
                    System.out.println("packet" + i + " has been lost!");   
                       
                }   
                   
            }   
            //Get the stop time   
            Calendar endtime = Calendar.getInstance();   
            Date d1 = begintime.getTime();   
            Date d2 = endtime.getTime();   
            long l1 = d1.getTime();   
            long l2 = d2.getTime();   
            double differ = Math.abs(l2-l1);   
            //calculate the average time   
            double average = differ/1000;   
            System.out.println(total + " packets have been received.");   
            System.out.println("The average time is :" + average+"ms");   
            System.out.println("The times packets have been resend is " + duplicated + ".");   
        }   
        catch (IOException ioe){   
   
            System.out.println("Socket error " + ioe);   
        }   
    }   
}   